//
//  NIMRightRedMoneyCell.m
//  QianbaoIM
//
//  Created by fengsh on 15/12/2.
//  Copyright © 2015年 qianbao.com. All rights reserved.
//

#import "NIMRightRedMoneyCell.h"

@implementation NIMRightRedMoneyCell

- (void)awakeFromNib {
     
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateWithRecordEntity:(ChatEntity *)recordEntity
{
    [super updateWithRecordEntity:recordEntity];
    
    [self.iconView setImage:IMGFROMBUNDLE(@"hongbao_bg")];
    
    UIImage *img = IMG2XFROMBUNDLE(@"image_mask_sender");
//    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(10,10, 10, 10) resizingMode:UIImageResizingModeStretch];
//
//    UIImageView *maskView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 220, 100)];
//    maskView.image = img;
//    maskView.frame = CGRectMake(0, 0, 220, 100);
//
//    self.iconView.maskView = maskView;
    
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.contents = (id)[img CGImage];
    shape.frame = CGRectMake(0, 0, 220, 100);
    self.iconView.layer.mask = shape;
    self.iconView.layer.masksToBounds = YES;
    
    CAShapeLayer *shape2 = [CAShapeLayer layer];
    shape2.contents = (id)[img CGImage];
    shape2.frame = CGRectMake(0.5, 0, 221, 101);
    self.maskView.layer.mask = shape2;
    self.maskView.layer.cornerRadius = 10;
    self.maskView.layer.masksToBounds = YES;
    
    self.redmoneylogo.image = IMGFROMBUNDLE(@"hongbao_logo");
    
    self.lbTips.text = @"发红包啦!";
    
    NSString *detail = recordEntity.msgContent;
    
    NSData *dt = [detail dataUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *dic =  [NSJSONSerialization JSONObjectWithData:dt options:NSJSONReadingAllowFragments error:nil];
    
    self.lbDescription.text = @"恭喜发财 吉祥如意!恭喜发财 吉祥如意!恭喜发财 吉祥如意!";
    
    self.redmoneyType.text = @"领取拼人品红包";
    
    if (dic) {
        
        self.lbDescription.text = dic[@"desc"];
        
        NSString *ty = dic[@"type"];
        if ([ty isEqualToString:@"LUCK"]||[ty isEqualToString:@"1"]) {//NORMAL:普通红包，LUCK:拼人品红包
            self.redmoneyType.text = @"领取拼人品红包";
        }
        else
        {
            self.redmoneyType.text = @"领取普通红包";
        }
    }
    
}

- (void)makeConstraints
{
    [super makeConstraints];

    [self.maskView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.paoButton.mas_top).with.offset(-0.5);
        make.trailing.equalTo(self.avatarBtn.mas_leading).with.offset(3);
        make.size.mas_equalTo(CGSizeMake(232, 102));
    }];
    
    [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.paoButton.mas_top).with.offset(0);
        make.trailing.equalTo(self.avatarBtn.mas_leading).with.offset(2);
        make.size.mas_equalTo(CGSizeMake(230, 100));
    }];
    
    [self.paoButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarBtn.mas_top);
        make.trailing.equalTo(self.iconView.mas_leading).with.offset(0);
        make.leading.equalTo(self.iconView.mas_leading);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(0);
    }];
    
    [self.redmoneylogo mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_top).offset(10);
        make.leading.equalTo(self.iconView.mas_leading).offset(10);
        make.width.equalTo(@36);
        make.height.equalTo(@47);
    }];
    
    [self.lbTips mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redmoneylogo.mas_top).offset(-2);
        make.leading.equalTo(self.redmoneylogo.mas_trailing).offset(10);
        make.trailing.equalTo(self.iconView.mas_trailing).offset(-20);
        make.height.equalTo(@30);
    }];
    
    [self.lbDescription mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lbTips.mas_bottom);
        make.leading.equalTo(self.lbTips.mas_leading);
        make.height.greaterThanOrEqualTo(@20);
        make.trailing.equalTo(self.lbTips.mas_trailing).offset(-10);
    }];
    
    [self.redmoneyType mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@0);
        make.height.equalTo(@30);
        make.leading.equalTo(self.iconView.mas_leading);
        make.trailing.equalTo(self.iconView.mas_trailing).offset(-20);
    }];
}

- (IBAction)recognizerHandler:(UIGestureRecognizer*)gesture
{
    
}

- (IBAction)tapRecognizerHandler:(UITapGestureRecognizer*)gesture
{
    [_delegate chatTableViewCell:self didSelectedWithRecordEntity:self.recordEntity];
}

- (UIImageView *)redmoneylogo
{
    if (!_redmoneylogo) {
        _redmoneylogo = [[UIImageView alloc]init];
        [self.iconView addSubview:_redmoneylogo];
    }
    
    return _redmoneylogo;
}

- (UILabel *)lbTips
{
    if (!_lbTips) {
        _lbTips = [[UILabel alloc]init];
        _lbTips.textColor = [UIColor whiteColor];
        _lbTips.font = [UIFont systemFontOfSize:20];
        [self.iconView addSubview:_lbTips];
    }
    
    return _lbTips;
}

- (UILabel *)lbDescription
{
    if (!_lbDescription) {
        _lbDescription = [[UILabel alloc]init];
        _lbDescription.textColor = [UIColor yellowColor];
        _lbDescription.numberOfLines = 1;
        _lbDescription.font = [UIFont systemFontOfSize:14];
        [self.iconView addSubview:_lbDescription];
    }
    
    return _lbDescription;
}

- (UILabel *)redmoneyType
{
    if (!_redmoneyType) {
        _redmoneyType = [[UILabel alloc]init];
        _redmoneyType.backgroundColor = [UIColor whiteColor];
        _redmoneyType.textColor = [UIColor redColor];
        _redmoneyType.clipsToBounds = YES;
        _redmoneyType.textAlignment = NSTextAlignmentCenter;
        _redmoneyType.font = [UIFont systemFontOfSize:15];
        [self.iconView addSubview:_redmoneyType];
    }
    
    return _redmoneyType;
}

- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _iconView.backgroundColor = [UIColor clearColor];
        _iconView.userInteractionEnabled = YES;
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        [_iconView addGestureRecognizer:self.tapGestureRecognizer];
        [_iconView addGestureRecognizer:self.longPressRecognizer];
        [self.contentView addSubview:_iconView];
    }
    return _iconView;
}


@end
